﻿using DryWCS.Model.SysConfig;
using DryWCS.Service.DbContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DryWCS.Service.SysConfig
{
    public class ConfigService : LocalDbContext<ConfigModel>
    {
        public ConfigModel GetByName(string configName)
        {
            try
            {
                return Db.Queryable<ConfigModel>()
                   .Where(it => it.ConfigName == configName).First();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public int UpdateByName(ConfigModel model)
        {
            try
            {
                return Db.Updateable<ConfigModel>(model).Where(p => p.ConfigName == model.ConfigName).IgnoreColumns("Description").ExecuteCommand();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public List<ConfigModel> GetListByLike(string search)
        {
            try
            {
                if (string.IsNullOrEmpty(search))
                {
                    return GetList();
                }
                else
                {
                    return Db.Queryable<ConfigModel>().
                        Where(it => it.ConfigName.Contains(search)
                        || it.ConfigValue.Contains(search)
                        || it.Description.Contains(search)).ToList();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}

